package com.shuyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuyu.common.core.domain.entity.Customer;
import com.shuyu.common.enums.ProductType;
import com.shuyu.common.exception.ServiceException;
import com.shuyu.common.utils.DesCode;
import com.shuyu.common.utils.SecurityUtils;
import com.shuyu.common.utils.StringUtils;
import com.shuyu.common.utils.aliyun.AliyunResponseBody;
import com.shuyu.common.utils.aliyun.Id2MetaVerifyUtil;
import com.shuyu.mapper.CustomerMapper;
import com.shuyu.service.CustomerService;
import com.shuyu.vo.Id2MetaVerifyVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

@Service
@Log4j2
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public void id2MetaVerify(Id2MetaVerifyVo id2MetaVerify) {
        try {
            AliyunResponseBody aliyunResponseBody = Id2MetaVerifyUtil.verify(id2MetaVerify.getIdentifyNum(), id2MetaVerify.getUserName());
            if(aliyunResponseBody.getCode().equals("200")){
                String bizCode = aliyunResponseBody.getResultObject().getBizCode();
                if(bizCode.equals("1")) {
                    Customer customer = new Customer();
                    Long userId = SecurityUtils.getLoginUser().getUserId();
                    customer.setId(userId);
                    customer.setIdCard(DesCode.encrypt(id2MetaVerify.getIdentifyNum()));
                    customer.setRealName(DesCode.encrypt(id2MetaVerify.getUserName()));
                    this.updateById(customer);
                }else if(bizCode.equals("2")){
                    throw new ServiceException("实名认证失败【核验不一致】");
                }else if(bizCode.equals("3")){
                    throw new ServiceException("实名认证失败【查无记录】");
                }else {
                    throw new ServiceException("实名认证失败【"+bizCode+"】");
                }
            }else {
                throw new ServiceException("实名认证失败【"+ aliyunResponseBody.getMessage()+"】");
            }
        }catch (Exception e){
            log.info("实名认证请求异常：{}",e.getMessage(),e.getCause());
            throw new ServiceException("系统异常！"+e.getMessage());
        }

    }

    @Override
//    产品类型1:NFT;2:卡牌;3:MV;4:内部权益;5:外部权益
    public void updateCustomerAssets(int type, int productId, long customerId) {
//        Customer customer = this.getById(customerId);
//        if(type == ProductType.NFT.type){
//            String nft = getProductIds(productId, customer.getNft());
//            this.update(new LambdaUpdateWrapper<Customer>().set(Customer::getNft,nft).eq(Customer::getId, customerId));
//        }else if(type == ProductType.卡牌.type){
//            String card = getProductIds(productId, customer.getCard());
//            this.update(new LambdaUpdateWrapper<Customer>().set(Customer::getCard,card).eq(Customer::getId, customerId));
//        }else if(type == ProductType.MV.type){
//            String mv = getProductIds(productId, customer.getMv());
//            this.update(new LambdaUpdateWrapper<Customer>().set(Customer::getMv,mv).eq(Customer::getId, customerId));
//        }else if(type == ProductType.内部权益.type){
//            String internalAssets = getProductIds(productId, customer.getInternalAssets());
//            this.update(new LambdaUpdateWrapper<Customer>().set(Customer::getInternalAssets,internalAssets).eq(Customer::getId, customerId));
//        }else if(type == ProductType.外部权益.type){
//            String externalAssets = getProductIds(productId, customer.getExternalAssets());
//            this.update(new LambdaUpdateWrapper<Customer>().set(Customer::getExternalAssets,externalAssets).eq(Customer::getId, customerId));
//        }
    }
    private String getProductIds(int productId, String productIds){
        if(StringUtils.isNotEmpty(productIds)){
            return productIds+","+productId;
        }else {
            return ""+productId;
        }
    }
}
